package com.example.wishstar.db;


import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.wishstar.model.Habit;
import com.example.wishstar.model.Memory;
import com.example.wishstar.model.OldHabit;
import com.example.wishstar.model.OldWish;
import com.example.wishstar.model.Star;
import com.example.wishstar.model.StarFlag;
import com.example.wishstar.model.User;
import com.example.wishstar.model.Users;
import com.example.wishstar.model.Wish;

import java.util.List;

@Dao
public interface UserDao
{
    /**
     *User
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertUser(User user);

    @Delete
    void deleteStudent(User user);

    @Query("SELECT * FROM user WHERE state = :state")
    List<User> getUserByState(int state);

    @Query("UPDATE user SET state = :state WHERE user_id = :user_id")
    void updateStateByuserId(int state, int user_id);
    /**
     * Users
     */

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertUsers(Users users);

    @Delete
    void deleteUsers(Users users);

    @Query("SELECT * FROM users WHERE userId = :userId")
    LiveData<Users> getUsersByUserId(int userId);


    /**
     * Star
     */
    @Insert(onConflict =  OnConflictStrategy.REPLACE)
    void insertStar(Star star);

    @Delete
    void deleteStar(Star star);

    @Query("SELECT * FROM star")
    List<Star> getStarList();

    @Update
    void updateStar(Star star);

    @Query("SELECT * FROM star WHERE wishId = :wishId")
    Star getStarByWishId(int wishId);

    /**
     * StarFlag
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertStarFlag(StarFlag starFlag);

    @Delete
    void deleteStarFlag(StarFlag starFlag);

    @Query("SELECT * FROM starflag")
    List<StarFlag> getAllStarFlag();

    @Query("SELECT * FROM starflag WHERE flag = :flag")
    List<StarFlag> getStarFlagByflag(int flag);

    @Query("UPDATE starflag SET flag = :flag WHERE starflagId = :starflagId")
    void updateStarFlag(int flag, int starflagId);

    @Query("SELECT * FROM starflag WHERE userId = :userId AND wishId = :wishId")
    List<StarFlag> getStarFlagByUserIdAndWishId(int userId, int wishId);

    /**
     * Memory
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertMemory(Memory memory);

    @Delete
    void deleteMemory(Memory memory);

    @Query("SELECT * FROM memory WHERE userId = :userId")
    LiveData<List<Memory>> getMemoryByuserId(int userId);

    @Query("DELETE FROM memory WHERE memoryId = :memoryId")
    void deleteMemoryByMemoryId(int memoryId);

    @Query("DELETE FROM memory WHERE userId = :userId")
    void deleteMemoryByUserId(int userId);

    /**
     *Habit
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertHabit(Habit habit);

    @Delete
    void deleteHabit(Habit habit);

    @Query("SELECT * FROM habit WHERE userId = :userId")
    LiveData<List<Habit>> getHabitByuserId(int userId);

    @Query("DELETE FROM habit WHERE habitId = :habitId")
    void deleteHabitByHabitId(int habitId);

    @Query("DELETE FROM habit WHERE userId = :userId")
    void deleteHabitByUserId(int userId);

    /**
     * Wish
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertWish(Wish wish);

    @Delete
    void deleteWish(Wish wish);

    @Query("SELECT * FROM wish WHERE userId = :userId")
    LiveData<List<Wish>> getWishByuserId(int userId);

    @Query("DELETE FROM wish WHERE wishId = :wishId")
    void deleteWishByWishId(int wishId);

    @Query("DELETE FROM wish WHERE userId = :userId")
    void deleteWishByUserId(int userId);

    /**
     * OldHabit
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertOldHabit(OldHabit oldHabit);

    @Delete
    void deleteOldHabit(OldHabit oldHabit);

    @Query("SELECT * FROM oldhabit WHERE userId = :userId")
    LiveData<List<OldHabit>> getOldHabitByuserId(int userId);

    @Query("DELETE FROM oldhabit WHERE habitId = :habitId")
    void deleteOldHabitByHabitId(int habitId);

    /**
     * OldWish
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertOldWish(OldWish oldWish);

    @Delete
    void deleteOldWish(OldWish oldWish);

    @Query("SELECT * FROM oldwish WHERE userId = :userId")
    LiveData<List<OldWish>> getOldWishByuserId(int userId);

    @Query("DELETE FROM oldwish WHERE wishId = :wishId")
    void deleteOldWishByWishId(int wishId);

}
